<?php

namespace App\Http\Controllers\Home;

use App\Models\Home\Topic;
use App\Models\Home\User;
use App\Models\Home\Weibo;
use App\Models\Home\UserInfo;
use App\Models\RTresult;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Session;

class WeiboController extends Controller
{
    //微博详情
    public function show($weibo_id)
    {
        $data = Weibo::join('user_infos', 'user_infos.user_id', '=', 'weibos.user_id')
            ->select('weibos.*', 'user_infos.user_id', 'user_infos.nickname', 'user_infos.avatar')
            ->where('weibos.id', $weibo_id)
            ->first()
            ->toArray();
        $a = UserInfo::select('favorites', 'likes')
            ->where('user_id', '=', Session::get('user.id'))
            ->first()
            ->toArray();
        $data['favorites'] = $a['favorites'];
        $data['likes'] = $a['likes'];
        return view('home.weibo_show', ['data' => $data]);
    }

    //发微博和转发微博
    public function store(Request $request)
    {
        $rt_result = new RTresult();
        $weibo = new Weibo();
        $weibo->user_id = Session::get('user.id');
        //如果有weibo_id,说明是转发的微博
        if (isset($request->weibo_id)) {
            //微博内容为转发框里输入的内容
            $weibo->content = $request->forward_content;
            //is_forward字段为原微博id
            $weibo->is_forward = $request->weibo_id;
            //原微博转发数+1
            Weibo::find($request->weibo_id)->increment('forward_num');
        } else {
            //否则是新微博

            //正则匹配#话题#
            $topic_pattern = '/\#([^\#|.]+)\#/';
            if (preg_match_all($topic_pattern, $request->weibo_content, $topic_arr)) {
                //微博内容匹配到#话题#
                $tag = implode(',', $topic_arr[1]);
                $weibo_content = preg_replace($topic_pattern, '', $request->weibo_content);
                //操作话题表
                $topic = new Topic();
                $res = Topic::where('topic', $tag)->first();
                //如果已经存在这个话题
                if ($res != null) {
                    //这个话题的微博数+1
                    $res->increment('weibo_num');
                    //话题id和话题内容填入微博表
                    $weibo->topic_id = $res->id;
                    $weibo->topic = '#' . $res->topic . '#';
                } else {
                    //否则新建一个话题
                    $topic->topic = $tag;
                    //这个话题的微博数+1
                    $topic->weibo_num = 1;
                    $topic->create_at = getTimeFormat();
                    $topic->save();
                    //话题id和话题内容填入微博表
                    $weibo->topic_id = $topic->id;
                    $weibo->topic = '#' . $topic->topic . '#';
                }
                //有话题, 微博内容为除去#话题#后的内容
                $weibo->content = $weibo_content;
            } else {
                //没有话题, 微博内容为输入框内容
                $weibo->content = $request->weibo_content;
            }

        }
        $weibo->created_at = getTimeFormat();
        if ($weibo->save()) {
            $rt_result->status = true;
            $rt_result->message = $weibo->id;
            //return $rt_result->toJson();
            return redirect('/');
        }
    }

    //某个微博的转发列表
    public function forward($weibo_id)
    {
        $data = Weibo::join('user_infos', 'user_infos.user_id', '=', 'weibos.user_id')
            ->select('weibos.*', 'user_infos.nickname', 'user_infos.avatar')
            ->where('weibos.is_forward', $weibo_id)
            ->get();
        $rt_result = new RTresult();
        if ($data) {
            $rt_result->status = true;
            $rt_result->message = $data;
            return $rt_result->toJson();
        }
    }

    //用户个人中心 默认显示微博列表
    public function index($user_id)
    {
        //取出用户信息
        $info = UserInfo::where('user_id', $user_id)->get()->first();
        //获取我的关注
        $follows = UserInfo::where('user_id', Session::get('user.id'))->get()->first()->following;
        $follows = explode(',', $follows);
        //用户的所有微博
        $records = Weibo::leftJoin('user_infos', 'user_infos.user_id', '=', 'weibos.user_id')
            ->select('weibos.*', 'user_infos.user_id', 'user_infos.nickname', 'user_infos.avatar')
            ->where('weibos.is_show', 1)
            ->where('weibos.user_id', $user_id)
            ->orderBy('created_at', 'desc')
            ->get()
            ->toArray();
        $a = UserInfo::select('favorites', 'likes')
            ->where('user_id', '=', $user_id)
            ->first()
            ->toArray();
        foreach ($records as $k => $v) {
            if ($v['is_forward']) {
                $forward_weibo = Weibo::join('user_infos', 'user_infos.user_id', '=', 'weibos.user_id')
                    ->select('weibos.*', 'user_infos.user_id', 'user_infos.nickname')
                    ->where('weibos.id', $v['is_forward'])
                    ->first()
                    ->toArray();
                $records[$k]['is_forward'] = $forward_weibo;
            }
            $records[$k]['favorites'] = $a['favorites'];
            $records[$k]['likes'] = $a['likes'];
        }
        return view('home.profile', ['info' => $info, 'follows' => $follows, 'records' => $records]);
    }
}
